package com.qf.controller;

import com.qf.enums.InterfaceExceptionEnums;
import com.qf.form.SmsInterfaceForm;
import com.qf.service.SmsService;
import com.qf.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/sms")
@Slf4j
public class SMSInterfaceController {

    @Autowired
    SmsService smsService;

    //获取 yml中的 ip-header 的数据，并存入 List集合中
    @Value("${ip-header}")
    private List<String> ipHeader;

    @RequestMapping("/smsInterface")
    public Result smsInterface(SmsInterfaceForm interfaceForm, HttpServletRequest request){
        //获取真实的ip地址
        String ip = this.getRealIp(request);
        log.info("[接口模块]  ip = {}", ip);
        interfaceForm.setIp(ip);

        //数据校验（判断是否为空）（永远不要相信前端）
        Result result = this.checkData(interfaceForm);
        if(result != null) {
            return  result;
        }

        //继续操作
        result = smsService.sendSMS(interfaceForm);


        return result;
    }

    private Result checkData(SmsInterfaceForm interfaceForm) {
        String clientID = interfaceForm.getClientID();
        //如果客户端id为空，返回 前端
        if(StringUtils.isEmpty(clientID)) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_CLIENT_ID_ERROR);
        }
        String content = interfaceForm.getContent();
        if(StringUtils.isEmpty(content)) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_CONTENT_ERROR);
        }
        String mobile = interfaceForm.getMobile();
        if(StringUtils.isEmpty(mobile)) {
            return Result.error(105, "手机号错误");
        }
        String pwd = interfaceForm.getPwd();
        if(StringUtils.isEmpty(pwd)) {
            return Result.error(102, "密码错误");
        }
        String srcID = interfaceForm.getSrcID();
        if(StringUtils.isEmpty(srcID)) {
            return Result.error(106, "下行编号(src)错误");
        }

        String ip = interfaceForm.getIp();
        if(StringUtils.isEmpty(ip)) {
            return Result.error(103, "ip校验错误");
        }


        return null;

    }

    //获取真实的ip
    private String getRealIp(HttpServletRequest request) {

        String ip = null;

        //判断 如果集合不为空，或长度不为 0
        if(!CollectionUtils.isEmpty(ipHeader)) {
            //不为空，则 循环 ipHeader字段， 获取 真实ip地址
            for (String key : ipHeader) {
                ip = request.getHeader(key);
                // 如果ip不为空， 表示已经获取ip，则返回
                if(!StringUtils.isEmpty(ip)) {
                    return ip;
                }
            }
        }
        //如果找不到ip，则 直接获取  RemoteAddr
        if(StringUtils.isEmpty(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
